package twentyThreePattern.StructuralPattern7.proxy.DynamicProxy.section2;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy {
	Object obj = null;

	public DynamicProxy(Object obj) {
		Class clazz = obj.getClass();
		this.obj = Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new MyInvocationHandler(obj));
	}

	public Object execute(String methodName, String... agrs) throws Exception {
		Class<?>[] types = new Class<?>[agrs.length];
		int i = 0;
		for (Object o : agrs) {
			types[i] = o.getClass();
			i++;
		}

		Method method = obj.getClass().getMethod(methodName, types);
		Object result = null;
		result = method.invoke(obj, agrs);
		return result;
	}

}
